<?php

namespace App\Admin\Controllers;

use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class UserController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '用户';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User());

        $grid->column('nick_name', __('Nick name'));
        $grid->column('avatar', __('Avatar'))->image('', 100);
        $grid->column('mobile', __('Mobile'));
        $grid->column('created_at', __('Created at'))->sortable();
        $grid->column('updated_at', '上次登录时间')->sortable();

        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->like('nick_name', __('Nick name'));
            $filter->like('mobile', __('Mobile'));
        });
        $grid->disableCreateButton();
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('openid', __('Openid'));
        $show->field('nick_name', __('Nick name'));
        $show->field('avatar', __('Avatar'));
        $show->field('unionid', __('Unionid'));
        $show->field('member_id', __('Member id'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('deleted_at', __('Deleted at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User());

        $form->text('openid', __('Openid'));
        $form->text('nick_name', __('Nick name'));
        $form->image('avatar', __('Avatar'));
        $form->text('unionid', __('Unionid'));
        $form->number('member_id', __('Member id'));

        return $form;
    }
}
